package com.wandian.news.adapter;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.bean.WXFeaturedBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.bytedance.sdk.openadsdk.TTNativeExpressAd;


/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description 适配器
 **/
public class WXFeaturedRecycleAdapter extends BaseQuickAdapter<WXFeaturedBean.NewslistEntity, BaseViewHolder> {

    private String value;
    private List<TTNativeExpressAd> ads = new ArrayList<>();
    public WXFeaturedRecycleAdapter(int layoutResId, String type) {
        super(layoutResId);
        this.value = type;
    }
    public List<TTNativeExpressAd> getAds() {
        return ads;
    }

    public void setAds(List<TTNativeExpressAd> ads) {
        this.ads = ads;
    }

    @Override
    protected void convert(BaseViewHolder helper, WXFeaturedBean.NewslistEntity item) {
        RelativeLayout rl_item = helper.getView(R.id.rl_item);
        try {
            ImageView iv_zx_1 = helper.getView(R.id.iv_zx_1);
            if(!UIUtils.isEmpty(item.getPicUrl())){
                GlideUtil.intoNewsPic(mContext,item.getPicUrl(),iv_zx_1);
            }
            TextView tv_time = helper.getView(R.id.tv_time);
            TextView tv_form = helper.getView(R.id.tv_form);
            TextView tv_zx_title = helper.getView(R.id.tv_zx_title);
            if(!UIUtils.isEmpty(item.getCtime())){
                if(item.getCtime().length()>10){
                    tv_time.setText(""+item.getCtime().substring(0,10));
                }else {
                    tv_time.setText(""+item.getCtime());
                }
            }
            if(!UIUtils.isEmpty(item.getDescription())){
                tv_form.setText(item.getDescription()+"");
            }
            if(!UIUtils.isEmpty(item.getTitle())){
                tv_zx_title.setText(item.getTitle());
            }

            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!UIUtils.isEmpty(item.getUrl())){
                        Bundle bundle = new Bundle();
                        bundle.putString("url",item.getUrl());
                        bundle.putString("title",item.getTitle());
                        bundle.putString("type","微信头条");
                        ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_WEB).with(bundle).navigation();
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(helper.getLayoutPosition()>0 && helper.getLayoutPosition()%3==0){
            if(!UIUtils.isEmpty(ads) && ads.size()>0){
                    Log.d("getLayoutPosition","getLayoutPosition"+helper.getLayoutPosition());
                    try {
                        int index = 0;
                        Random random = new Random();
                        index =random.nextInt(ads.size());
                        CSJADTools.bindAdListener(ads.get(index),rl_item);
                        ads.get(index).render();
                        notifyItemChanged(helper.getLayoutPosition());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        }


    }


}
